package com.ygqh.baby.service.impl;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import  org.springframework.beans.factory.annotation.Autowired;
import  org.springframework.stereotype.Service;
import  org.springframework.util.Assert;

import  com.ygqh.baby.ao.DataStatus;
import  com.ygqh.baby.ao.QueryInfo;
import  com.ygqh.baby.ao.ResultSet;
import  com.ygqh.baby.mapper.AdminUserMapper;
import com.ygqh.baby.mapper.AdminUserRoleMapper;
import com.ygqh.baby.po.AdminPermission;
import com.ygqh.baby.po.AdminRole;
import  com.ygqh.baby.po.AdminUser;
import  com.ygqh.baby.po.AdminUserExample;
import com.ygqh.baby.po.AdminUserRole;
import com.ygqh.baby.po.AdminUserRoleExample;
import  com.ygqh.baby.service.AdminUserService;

@Service
public class AdminUserServiceImpl implements AdminUserService{
	
	@Autowired
	private AdminUserMapper adminUserMapper;
	@Autowired
	private AdminUserRoleMapper userRoleMapper;
	
	
	@Override
	public List<AdminUser> find() {
		AdminUserExample example = new AdminUserExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return adminUserMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		AdminUserExample example = new AdminUserExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return adminUserMapper.countByExample(example);
	}

	@Override
	public AdminUser findById(Long id) {
		return adminUserMapper.selectByPrimaryKey(id);
	}
	

	@Override
	public int save(AdminUser adminUser) {
		new PasswordHelper().encryptPassword(adminUser);
		
		int r = adminUserMapper.insertSelective(adminUser);
		for(AdminRole role: adminUser.getRoles()) {
			AdminUserRole userRole = new AdminUserRole();
			userRole.setRoleId(role.getId());
			userRole.setUserId(adminUser.getId());
			userRoleMapper.insertSelective(userRole);
		}
		return r;
	}

	@Override
	public int update(AdminUser adminUser) {
		new PasswordHelper().encryptPassword(adminUser);
		//更新用户权限
		adminUserMapper.updateRoleStatusByUserId(adminUser.getId(), DataStatus.Valid.name());
		for(AdminRole role: adminUser.getRoles()) {
			AdminUserRole userRole = new AdminUserRole();
			userRole.setRoleId(role.getId());
			userRole.setUserId(adminUser.getId());
			userRoleMapper.insertSelective(userRole);
		}
		return adminUserMapper.updateByPrimaryKeySelective(adminUser);
	}

	@Override
	public ResultSet<AdminUser> search(QueryInfo queryInfo,String q) {
		List<AdminUser>  list =  adminUserMapper.selectSuper(queryInfo, q);
		int count = adminUserMapper.countSuper(q);
		ResultSet<AdminUser> resultSet = new ResultSet<AdminUser>(count,list);
		return resultSet;
	}

	@Override
	public int deleteSoft(Long[] ids,String adminUserName) {
		Assert.notEmpty(ids);
		AdminUserExample example = new AdminUserExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		AdminUser record = new AdminUser();
		record.setStatus(DataStatus.Delete);
		record.setUpdateTime(new Date());
		record.setUpdateBy(adminUserName);
		return adminUserMapper.updateByExampleSelective(record, example);
	}
	
	@Override
	public int deleteSoft(Long id,String adminUserName) {
		return this.deleteSoft(new Long[]{id},adminUserName);
	}

	@Override
	public int saveOrUpdate(AdminUser adminUser) {
		int r = 0;
		if(adminUser.getId() != null) {
			r = this.update(adminUser);
		}else {
			r = this.save(adminUser);
		}
		return r;
	}

	@Override
	public AdminUser findByUserName(String username) {
		
		AdminUserExample example = new AdminUserExample();
		example.createCriteria().andUsernameEqualTo(username).andStatusEqualTo(DataStatus.Valid.name());
	 	List<AdminUser> user =  adminUserMapper.selectByExample(example);
		if(user != null && user.size() == 1) {
			return user.get(0);
		}
	 	return null;
	}

	@Override
	public List<AdminPermission> findMenuByUser(Long userId) {
		return adminUserMapper.findMenuByUser(userId);
	}

	@Override
	public List<AdminPermission> findPermissionByUser(Long userId) {
		return adminUserMapper.findPermissionByUser(userId);
	}

	@Override
	public List<AdminRole> findRolesByUser(Long userId) {
		return adminUserMapper.findRolesByUser(userId);
	}

	@Override
	public int save(AdminUser adminUser, Long[] roleIds) {
		this.save(adminUser);
		AdminUserRole userRole = new AdminUserRole();
		int count = 0;
		for(Long roleId:roleIds) {
			userRole.setRoleId(roleId);
			userRole.setUserId(adminUser.getId());
			count+=userRoleMapper.insertSelective(userRole);
		}
		return count;
	}

	

	

}

